/*
 * 文件名：DeleteDeviceServiceImpl.java
 * 版权：Copyright by www.tiansu-china.com/
 * 描述：
 * 修改人：congriran
 * 修改时间：2021年2月1日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
*/

package com.tiansu.reborn.wanda.modules.executor.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.tiansu.reborn.wanda.modules.executor.dao.DeleteDeviceDao;
import com.tiansu.reborn.wanda.modules.executor.service.DeleteDeviceService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DeleteDeviceServiceImpl implements DeleteDeviceService
{
    @Autowired
    private DeleteDeviceDao deleteDeviceDao;

    @Transactional
    @Override
    public int deleteDevice(Integer deviceId, String deviceNo)
    {
        if (null == deviceId)
        {
            if (!StringUtils.isEmpty(deviceNo))
            {
                deviceId = deleteDeviceDao.queryDeviceId(deviceNo);
                if (null == deviceId)
                {
                    log.error("DeviceId does not exist!");
                    return -1;
                }
            }
            else
            {
                return -1;
            }
        }
        int num = 0;
        log.debug("Delete device begin! DeviceId is: " + deviceId);
        num += deleteDeviceDao.deleteDeviceProperty(deviceId);
        num += deleteDeviceDao.deletePaymentDeviceEx(deviceId);
        num += deleteDeviceDao.deleteDI(deviceId);
        num += deleteDeviceDao.deletePulse(deviceId);
        num += deleteDeviceDao.deleteControl(deviceId * 100 + 1);
        num += deleteDeviceDao.deleteLPaymentRoom(deviceId);
        num += deleteDeviceDao.deletePaymentRoom(deviceId + 10000000);
        num += deleteDeviceDao.deletePaymentVirtualDevice(deviceId);
        num += deleteDeviceDao.deleteSampleday(deviceId);
        num += deleteDeviceDao.deleteSampledayExt(deviceId);
        num += deleteDeviceDao.deleteSamplehour(deviceId);
        num += deleteDeviceDao.deleteSamplehourExt(deviceId);
        num += deleteDeviceDao.deleteDevice(deviceId);
        log.debug("Delete device end! DeviceId is: " + deviceId + "A total of "+num+" device data were deleted");
        return num;
    }

}
